package ysq.xyg.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ysq.xyg.dao.CartDao;
import ysq.xyg.model.Cart;
import ysq.xyg.model.Item;
import ysq.xyg.model.info.ReturnInfo;
import ysq.xyg.service.CartService;
import ysq.xyg.service.ItemService;
import ysq.xyg.service.WinNumService;
import ysq.xyg.util.BaseConstant;

import javax.annotation.Resource;
import java.util.List;

/**
 * 购物车
 *
 * @author congcong tao
 * @create 2018/8/9 下午8:52
 */
@Service("cartService")
public class CartServiceImpl extends BaseServiceImpl<Cart> implements CartService {

    @Resource
    private WinNumService winNumService;
    @Resource
    private ItemService itemService;
    @Resource
    private CartDao cartDao;
    @Autowired
    public void setCartDao(CartDao cartDao) {
        this.baseDao = cartDao;
    }

    @Override
    public PageInfo<Cart> pageInfo(Cart cart, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Cart> list = cartDao.selectEntity(cart);
        return new PageInfo<Cart>(list);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class})
    public ReturnInfo emptyCart(String memberId, String cartJson) {
        ReturnInfo returnInfo = new ReturnInfo();
        JSONArray jsonArray = JSON.parseArray(cartJson);
        if (jsonArray == null || jsonArray.size() == 0) {
            returnInfo = new ReturnInfo(BaseConstant.FAIL, "请正确从购物车购买！");
            return returnInfo;
        }
        int size = jsonArray.size();
        for (int i = 0; i < size; i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String id = jsonObject.getString("id");
            String activityId = jsonObject.getString("activityId");
            String itemId = jsonObject.getString("itemId");
            String numStr = jsonObject.getString("num");
            Item item = itemService.selectByPrimaryKey(itemId);
            int num = Integer.parseInt(numStr);
            try {
                int flag = winNumService.winningNum(memberId, activityId, num,itemId);
                if (flag == -1) {
                    returnInfo = new ReturnInfo(BaseConstant.FAIL_CODE_FOUR, "您购买" + item.getTitle() + "花费的钻石不足");
                    break;
                } else if (flag == 0) {
                    returnInfo = new ReturnInfo(BaseConstant.FAIL_CODE_FOUR, "购买" + item.getTitle() + "失败");
                    break;
                } else {
                    // 删除该条记录
                    this.deleteByPrimaryKey(id);
                }
            } catch (RuntimeException e) {
                returnInfo = new ReturnInfo(BaseConstant.FAIL_CODE_FOUR, "购买" + item.getTitle() + "失败");
            }
        }
        return returnInfo;
    }
}
